home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2000 March / maximum-cd-2000-03.iso / Quake3 Game Source / Q3AGameSource.exe / Main / g_target.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-18  |  11.8 KB  |  446 lines

  1. // Copyright (C) 1999-2000 Id Software, Inc.
  2. //
  3. #include "g_local.h"
  4.  
  5. //==========================================================
  6.  
  7. /*QUAKED target_give (1 0 0) (-8 -8 -8) (8 8 8)
  8. Gives the activator all the items pointed to.
  9. */
  10. void Use_Target_Give( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
  11.     gentity_t    *t;
  12.     trace_t        trace;
  13.  
  14.     if ( !activator->client ) {
  15.         return;
  16.     }
  17.  
  18.     if ( !ent->target ) {
  19.         return;
  20.     }
  21.  
  22.     memset( &trace, 0, sizeof( trace ) );
  23.     t = NULL;
  24.     while ( (t = G_Find (t, FOFS(targetname), ent->target)) != NULL ) {
  25.         if ( !t->item ) {
  26.             continue;
  27.         }
  28.         Touch_Item( t, activator, &trace );
  29.  
  30.         // make sure it isn't going to respawn or show any events
  31.         t->nextthink = 0;
  32.         trap_UnlinkEntity( t );
  33.     }
  34. }
  35.  
  36. void SP_target_give( gentity_t *ent ) {
  37.     ent->use = Use_Target_Give;
  38. }
  39.  
  40.  
  41. //==========================================================
  42.  
  43. /*QUAKED target_remove_powerups (1 0 0) (-8 -8 -8) (8 8 8)
  44. takes away all the activators powerups.
  45. Used to drop flight powerups into death puts.
  46. */
  47. void Use_target_remove_powerups( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
  48.     if ( !activator->client ) {
  49.         return;
  50.     }
  51.  
  52.     if ( activator->client->ps.powerups[PW_REDFLAG] ) {
  53.         Team_ReturnFlag(TEAM_RED);
  54.     } else if ( activator->client->ps.powerups[PW_BLUEFLAG] ) {
  55.         Team_ReturnFlag(TEAM_BLUE);
  56.     }
  57.  
  58.     memset( activator->client->ps.powerups, 0, sizeof( activator->client->ps.powerups ) );
  59. }
  60.  
  61. void SP_target_remove_powerups( gentity_t *ent ) {
  62.     ent->use = Use_target_remove_powerups;
  63. }
  64.  
  65.  
  66. //==========================================================
  67.  
  68. /*QUAKED target_delay (1 0 0) (-8 -8 -8) (8 8 8)
  69. "wait" seconds to pause before firing targets.
  70. "random" delay variance, total delay = delay +/- random seconds
  71. */
  72. void Think_Target_Delay( gentity_t *ent ) {
  73.     G_UseTargets( ent, ent->activator );
  74. }
  75.  
  76. void Use_Target_Delay( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
  77.     ent->nextthink = level.time + ( ent->wait + ent->random * crandom() ) * 1000;
  78.     ent->think = Think_Target_Delay;
  79.     ent->activator = activator;
  80. }
  81.  
  82. void SP_target_delay( gentity_t *ent ) {
  83.     // check delay for backwards compatability
  84.     if ( !G_SpawnFloat( "delay", "0", &ent->wait ) ) {
  85.         G_SpawnFloat( "wait", "1", &ent->wait );
  86.     }
  87.  
  88.     if ( !ent->wait ) {
  89.         ent->wait = 1;
  90.     }
  91.     ent->use = Use_Target_Delay;
  92. }
  93.  
  94.  
  95. //==========================================================
  96.  
  97. /*QUAKED target_score (1 0 0) (-8 -8 -8) (8 8 8)
  98. "count" number of points to add, default 1
  99.  
  100. The activator is given this many points.
  101. */
  102. void Use_Target_Score (gentity_t *ent, gentity_t *other, gentity_t *activator) {
  103.     AddScore( activator, ent->count );
  104. }
  105.  
  106. void SP_target_score( gentity_t *ent ) {
  107.     if ( !ent->count ) {
  108.         ent->count = 1;
  109.     }
  110.     ent->use = Use_Target_Score;
  111. }
  112.  
  113.  
  114. //==========================================================
  115.  
  116. /*QUAKED target_print (1 0 0) (-8 -8 -8) (8 8 8) redteam blueteam private
  117. "message"    text to print
  118. If "private", only the activator gets the message.  If no checks, all clients get the message.
  119. */
  120. void Use_Target_Print (gentity_t *ent, gentity_t *other, gentity_t *activator) {
  121.     if ( activator->client && ( ent->spawnflags & 4 ) ) {
  122.         trap_SendServerCommand( activator-g_entities, va("cp \"%s\"", ent->message ));
  123.         return;
  124.     }
  125.  
  126.     if ( ent->spawnflags & 3 ) {
  127.         if ( ent->spawnflags & 1 ) {
  128.             G_TeamCommand( TEAM_RED, va("cp \"%s\"", ent->message) );
  129.         }
  130.         if ( ent->spawnflags & 2 ) {
  131.             G_TeamCommand( TEAM_BLUE, va("cp \"%s\"", ent->message) );
  132.         }
  133.         return;
  134.     }
  135.  
  136.     trap_SendServerCommand( -1, va("cp \"%s\"", ent->message ));
  137. }
  138.  
  139. void SP_target_print( gentity_t *ent ) {
  140.     ent->use = Use_Target_Print;
  141. }
  142.  
  143.  
  144. //==========================================================
  145.  
  146.  
  147. /*QUAKED target_speaker (1 0 0) (-8 -8 -8) (8 8 8) looped-on looped-off global activator
  148. "noise"        wav file to play
  149.  
  150. A global sound will play full volume throughout the level.
  151. Activator sounds will play on the player that activated the target.
  152. Global and activator sounds can't be combined with looping.
  153. Normal sounds play each time the target is used.
  154. Looped sounds will be toggled by use functions.
  155. Multiple identical looping sounds will just increase volume without any speed cost.
  156. "wait" : Seconds between auto triggerings, 0 = don't auto trigger
  157. "random"    wait variance, default is 0
  158. */
  159. void Use_Target_Speaker (gentity_t *ent, gentity_t *other, gentity_t *activator) {
  160.     if (ent->spawnflags & 3) {    // looping sound toggles
  161.         if (ent->s.loopSound)
  162.             ent->s.loopSound = 0;    // turn it off
  163.         else
  164.             ent->s.loopSound = ent->noise_index;    // start it
  165.     }else {    // normal sound
  166.         if ( ent->spawnflags & 8 ) {
  167.             G_AddEvent( activator, EV_GENERAL_SOUND, ent->noise_index );
  168.         } else if (ent->spawnflags & 4) {
  169.             G_AddEvent( ent, EV_GLOBAL_SOUND, ent->noise_index );
  170.         } else {
  171.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->noise_index );
  172.         }
  173.     }
  174. }
  175.  
  176. void SP_target_speaker( gentity_t *ent ) {
  177.     char    buffer[MAX_QPATH];
  178.     char    *s;
  179.  
  180.     G_SpawnFloat( "wait", "0", &ent->wait );
  181.     G_SpawnFloat( "random", "0", &ent->random );
  182.  
  183.     if ( !G_SpawnString( "noise", "NOSOUND", &s ) ) {
  184.         G_Error( "target_speaker without a noise key at %s", vtos( ent->s.origin ) );
  185.     }
  186.  
  187.     // force all client reletive sounds to be "activator" speakers that
  188.     // play on the entity that activates it
  189.     if ( s[0] == '*' ) {
  190.         ent->spawnflags |= 8;
  191.     }
  192.  
  193.     if (!strstr( s, ".wav" )) {
  194.         Com_sprintf (buffer, sizeof(buffer), "%s.wav", s );
  195.     } else {
  196.         Q_strncpyz( buffer, s, sizeof(buffer) );
  197.     }
  198.     ent->noise_index = G_SoundIndex(buffer);
  199.  
  200.     // a repeating speaker can be done completely client side
  201.     ent->s.eType = ET_SPEAKER;
  202.     ent->s.eventParm = ent->noise_index;
  203.     ent->s.frame = ent->wait * 10;
  204.     ent->s.clientNum = ent->random * 10;
  205.  
  206.  
  207.     // check for prestarted looping sound
  208.     if ( ent->spawnflags & 1 ) {
  209.         ent->s.loopSound = ent->noise_index;
  210.     }
  211.  
  212.     ent->use = Use_Target_Speaker;
  213.  
  214.     if (ent->spawnflags & 4) {
  215.         ent->r.svFlags |= SVF_BROADCAST;
  216.     }
  217.  
  218.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  219.  
  220.     // must link the entity so we get areas and clusters so
  221.     // the server can determine who to send updates to
  222.     trap_LinkEntity( ent );
  223. }
  224.  
  225.  
  226.  
  227. //==========================================================
  228.  
  229. /*QUAKED target_laser (0 .5 .8) (-8 -8 -8) (8 8 8) START_ON
  230. When triggered, fires a laser.  You can either set a target or a direction.
  231. */
  232. void target_laser_think (gentity_t *self) {
  233.     vec3_t    end;
  234.     trace_t    tr;
  235.     vec3_t    point;
  236.  
  237.     // if pointed at another entity, set movedir to point at it
  238.     if ( self->enemy ) {
  239.         VectorMA (self->enemy->s.origin, 0.5, self->enemy->r.mins, point);
  240.         VectorMA (point, 0.5, self->enemy->r.maxs, point);
  241.         VectorSubtract (point, self->s.origin, self->movedir);
  242.         VectorNormalize (self->movedir);
  243.     }
  244.  
  245.     // fire forward and see what we hit
  246.     VectorMA (self->s.origin, 2048, self->movedir, end);
  247.  
  248.     trap_Trace( &tr, self->s.origin, NULL, NULL, end, self->s.number, CONTENTS_SOLID|CONTENTS_BODY|CONTENTS_CORPSE);
  249.  
  250.     if ( tr.entityNum ) {
  251.         // hurt it if we can
  252.         G_Damage ( &g_entities[tr.entityNum], self, self->activator, self->movedir, 
  253.             tr.endpos, self->damage, DAMAGE_NO_KNOCKBACK, MOD_TARGET_LASER);
  254.     }
  255.  
  256.     VectorCopy (tr.endpos, self->s.origin2);
  257.  
  258.     trap_LinkEntity( self );
  259.     self->nextthink = level.time + FRAMETIME;
  260. }
  261.  
  262. void target_laser_on (gentity_t *self)
  263. {
  264.     if (!self->activator)
  265.         self->activator = self;
  266.     target_laser_think (self);
  267. }
  268.  
  269. void target_laser_off (gentity_t *self)
  270. {
  271.     trap_UnlinkEntity( self );
  272.     self->nextthink = 0;
  273. }
  274.  
  275. void target_laser_use (gentity_t *self, gentity_t *other, gentity_t *activator)
  276. {
  277.     self->activator = activator;
  278.     if ( self->nextthink > 0 )
  279.         target_laser_off (self);
  280.     else
  281.         target_laser_on (self);
  282. }
  283.  
  284. void target_laser_start (gentity_t *self)
  285. {
  286.     gentity_t *ent;
  287.  
  288.     self->s.eType = ET_BEAM;
  289.  
  290.     if (self->target) {
  291.         ent = G_Find (NULL, FOFS(targetname), self->target);
  292.         if (!ent) {
  293.             G_Printf ("%s at %s: %s is a bad target\n", self->classname, vtos(self->s.origin), self->target);
  294.         }
  295.         self->enemy = ent;
  296.     } else {
  297.         G_SetMovedir (self->s.angles, self->movedir);
  298.     }
  299.  
  300.     self->use = target_laser_use;
  301.     self->think = target_laser_think;
  302.  
  303.     if ( !self->damage ) {
  304.         self->damage = 1;
  305.     }
  306.  
  307.     if (self->spawnflags & 1)
  308.         target_laser_on (self);
  309.     else
  310.         target_laser_off (self);
  311. }
  312.  
  313. void SP_target_laser (gentity_t *self)
  314. {
  315.     // let everything else get spawned before we start firing
  316.     self->think = target_laser_start;
  317.     self->nextthink = level.time + FRAMETIME;
  318. }
  319.  
  320.  
  321. //==========================================================
  322.  
  323. void target_teleporter_use( gentity_t *self, gentity_t *other, gentity_t *activator ) {
  324.     gentity_t    *dest;
  325.  
  326.     if (!activator->client)
  327.         return;
  328.     dest =     G_PickTarget( self->target );
  329.     if (!dest) {
  330.         G_Printf ("Couldn't find teleporter destination\n");
  331.         return;
  332.     }
  333.  
  334.     TeleportPlayer( activator, dest->s.origin, dest->s.angles );
  335. }
  336.  
  337. /*QUAKED target_teleporter (1 0 0) (-8 -8 -8) (8 8 8)
  338. The activator will be teleported away.
  339. */
  340. void SP_target_teleporter( gentity_t *self ) {
  341.     if (!self->targetname)
  342.         G_Printf("untargeted %s at %s\n", self->classname, vtos(self->s.origin));
  343.  
  344.     self->use = target_teleporter_use;
  345. }
  346.  
  347. //==========================================================
  348.  
  349.  
  350. /*QUAKED target_relay (.5 .5 .5) (-8 -8 -8) (8 8 8) RED_ONLY BLUE_ONLY RANDOM
  351. This doesn't perform any actions except fire its targets.
  352. The activator can be forced to be from a certain team.
  353. if RANDOM is checked, only one of the targets will be fired, not all of them
  354. */
  355. void target_relay_use (gentity_t *self, gentity_t *other, gentity_t *activator) {
  356.     if ( ( self->spawnflags & 1 ) && activator->client 
  357.         && activator->client->sess.sessionTeam != TEAM_RED ) {
  358.         return;
  359.     }
  360.     if ( ( self->spawnflags & 2 ) && activator->client 
  361.         && activator->client->sess.sessionTeam != TEAM_BLUE ) {
  362.         return;
  363.     }
  364.     if ( self->spawnflags & 4 ) {
  365.         gentity_t    *ent;
  366.  
  367.         ent = G_PickTarget( self->target );
  368.         if ( ent && ent->use ) {
  369.             ent->use( ent, self, activator );
  370.         }
  371.         return;
  372.     }
  373.     G_UseTargets (self, activator);
  374. }
  375.  
  376. void SP_target_relay (gentity_t *self) {
  377.     self->use = target_relay_use;
  378. }
  379.  
  380.  
  381. //==========================================================
  382.  
  383. /*QUAKED target_kill (.5 .5 .5) (-8 -8 -8) (8 8 8)
  384. Kills the activator.
  385. */
  386. void target_kill_use( gentity_t *self, gentity_t *other, gentity_t *activator ) {
  387.     G_Damage ( activator, NULL, NULL, NULL, NULL, 100000, DAMAGE_NO_PROTECTION, MOD_TELEFRAG);
  388. }
  389.  
  390. void SP_target_kill( gentity_t *self ) {
  391.     self->use = target_kill_use;
  392. }
  393.  
  394. /*QUAKED target_position (0 0.5 0) (-4 -4 -4) (4 4 4)
  395. Used as a positional target for in-game calculation, like jumppad targets.
  396. */
  397. void SP_target_position( gentity_t *self ){
  398.     G_SetOrigin( self, self->s.origin );
  399. }
  400.  
  401. static void target_location_linkup(gentity_t *ent)
  402. {
  403.     int i;
  404.     int n;
  405.  
  406.     if (level.locationLinked) 
  407.         return;
  408.  
  409.     level.locationLinked = qtrue;
  410.  
  411.     level.locationHead = NULL;
  412.  
  413.     trap_SetConfigstring( CS_LOCATIONS, "unknown" );
  414.  
  415.     for (i = 0, ent = g_entities, n = 1;
  416.             i < level.num_entities;
  417.             i++, ent++) {
  418.         if (ent->classname && !Q_stricmp(ent->classname, "target_location")) {
  419.             // lets overload some variables!
  420.             ent->health = n; // use for location marking
  421.             trap_SetConfigstring( CS_LOCATIONS + n, ent->message );
  422.             n++;
  423.             ent->nextTrain = level.locationHead;
  424.             level.locationHead = ent;
  425.         }
  426.     }
  427.  
  428.     // All linked together now
  429. }
  430.  
  431. /*QUAKED target_location (0 0.5 0) (-8 -8 -8) (8 8 8)
  432. Set "message" to the name of this location.
  433. Set "count" to 0-7 for color.
  434. 0:white 1:red 2:green 3:yellow 4:blue 5:cyan 6:magenta 7:white
  435.  
  436. Closest target_location in sight used for the location, if none
  437. in site, closest in distance
  438. */
  439. void SP_target_location( gentity_t *self ){
  440.     self->think = target_location_linkup;
  441.     self->nextthink = level.time + 200;  // Let them all spawn first
  442.  
  443.     G_SetOrigin( self, self->s.origin );
  444. }
  445.  
  446.